import {Injectable} from '@angular/core';
import {BaThemeConfigProvider, colorHelper} from '../../../../theme';
import {UserService} from '../../../../services/app.user';
import {Utility} from '../../../../services/app.utility';

@Injectable()
export class TrafficChartService {

    public loginPage;
    public dashBoard;
    public postIdea;
    public listIdea;
    public leaderBoard;
    public recentIdeas;
    public topAllTime;
    public adminUser;
    public adminReportedAbuse;
    public adminAddUser;
    public adminVersioning;
    public ideaPage;
    public viewProfile;
    public totalCount;
    public text;
    public userName;
    public userIdForTimeline;
    public citationTree;
    public allData;
    public arrayForGraph: any = [];
    public graphArray: any = [];
    public totalCountOfTimeline: number = 0;

    constructor(private _baConfig:BaThemeConfigProvider, private userService:UserService, private utility:Utility) {
        this.getTimeline();
    }

    // Function for API call to get user info
    public getTimeline() {
        this.userService.getuserInfo().subscribe(
            data => this.userInfoSuccess(data),
            error => this.userInfoFail(error)
        );
    }

    // Function for API call to get user id
    public userInfoSuccess(data) {
      console.log("traffice chart");

        let input;

        if (this.utility.userDetailFlag) {
            input = {
                userId: this.utility.userDetails._id
            };
            this.userName = this.utility.userDetails.name;
            this.userIdForTimeline = this.utility.userDetails._id;
        } else {
            input = {
                userId: data._id
            };
            this.userName = data.firstName;
          this.userIdForTimeline = data._id;
        }

        this.utility.userDetailFlag = false;

        this.userService.getTimelineData(input).subscribe(
            data => this.timelineSuccess(data),
            error => this.timelineFail(error)
        );
    }

    public timelineSuccess(data) {
      this.allData = data[0];
      if (data.length > 0) {
        let dashboardColors = this._baConfig.get().colors.dashboard;

        this.text = 'Total Time';

      } else {
        this.totalCount = 0;
        this.text = 'No Data';
      }


      let dashboardColors = this._baConfig.get().colors.dashboard;
      let colors = [];
      colors[0] = dashboardColors.blueStone;
      colors[1] = dashboardColors.surfieGreen;
      colors[2] = dashboardColors.silverTree;
      colors[3] = dashboardColors.gossip;
      colors[4] = dashboardColors.green;
      colors[5] = dashboardColors.red;
      colors[6] = dashboardColors.lightPink;
      colors[7] = dashboardColors.grey;
      colors[8] = dashboardColors.yellow;
      colors[9] = dashboardColors.orange;
      colors[10] = dashboardColors.salmon;
      colors[11] = dashboardColors.maroon;
      colors[12] = dashboardColors.lightgrey;
      colors[13] = dashboardColors.lemon;



      for(let i = 0; i < this.allData.items.length; i++) {


        this.totalCountOfTimeline += this.allData.items[i].timeSpent;
        if(this.allData.items[i].pageName === 'login') {
          let input = {
            count: i,
            color: colors[5],
            name: 'Login'
          };
          this.addArrayObject(input);
        } else if(this.allData.items[i].pageName === 'postIdea') {
          let input = {
            count: i,
            color: colors[0],
            name: 'Post An Idea'
          };
          this.addArrayObject(input);
        } else if(this.allData.items[i].pageName === 'listIdea') {
          let input = {
            count: i,
            color: colors[1],
            name: 'ListIdea'
          };
          this.addArrayObject(input);
        } else if(this.allData.items[i].pageName === 'leaderboard') {
          let input = {
            count: i,
            color: colors[2],
            name: 'Leaderboard'
          };
          this.addArrayObject(input);
        } else if(this.allData.items[i].pageName === 'recentIdea') {
          let input = {
            count: i,
            color: colors[3],
            name: 'RecentIdea'
          };
          this.addArrayObject(input);
        } else if(this.allData.items[i].pageName === 'topAllTime') {
          let input = {
            count: i,
            color: colors[12],
            name: 'TopAllTime'
          };
          this.addArrayObject(input);
        } else if(this.allData.items[i].pageName === 'admin/user') {
          let input = {
            count: i,
            color: colors[4],
            name: 'Users'
          };
          this.addArrayObject(input);
        } else if(this.allData.items[i].pageName === 'admin/addUser') {
          let input = {
            count: i,
            color: colors[11],
            name: 'AddUser'
          };
          this.addArrayObject(input);
        } else if(this.allData.items[i].pageName === 'admin/timeline') {
          let input = {
            count: i,
            color: colors[6],
            name: 'Timeline'
          };
          this.addArrayObject(input);
        } else if(this.allData.items[i].pageName === 'admin/activity') {
          let input = {
            count: i,
            color: colors[7],
            name: 'Activity'
          };
          this.addArrayObject(input);
        } else if(this.allData.items[i].pageName === 'admin/versioning') {
          let input = {
            count: i,
            color: colors[8],
            name: 'Versioning'
          };
          this.addArrayObject(input);
        } else if(this.allData.items[i].pageName === 'admin/abuseList') {
          let input = {
            count: i,
            color: colors[9],
            name: 'Reported Abuse'
          };
          this.addArrayObject(input);
        } else if(this.allData.items[i].pageName === 'admin/userlog') {
          let input = {
            count: i,
            color: colors[9],
            name: 'User Log'
          };
          this.addArrayObject(input);
        } else if(this.allData.items[i].pageName === 'dashboard') {
          let input = {
            count: i,
            color: colors[10],
            name: 'Dashboard'
          };
          this.addArrayObject(input);
        }  else if(this.allData.items[i].pageName === 'logout') {
          let input = {
            count: i,
            color: colors[13],
            name: 'Logout'
          };
          this.addArrayObject(input);
        }
      }
      this.totalCount = this.totalCountOfTimeline;
      this.arrayForGraph[0] = this.graphArray;
    }

    public addArrayObject(input) {

      this.graphArray[input.count] = {
        value: this.allData.items[input.count].timeSpent,
        color: input.color,
        highlight: colorHelper.shade(input.color, 15),
        label: input.name,
        percentage: (this.allData.items[input.count].timeSpent * 100) / this.totalCountOfTimeline,
        order: input.count
      };


    }

  // function for timeline data and pass to graph
  getData() {
    return this.arrayForGraph[0];

  }



    // Function call fail timeline data
    public timelineFail(err) {
    }

    // function call fail get user info
    public userInfoFail(err) {
    }


}
